package com.tjetc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tjetc.entity.*;
import com.tjetc.mapper.TribeMapper;
import com.tjetc.mapper.TribeMessageMapper;
import com.tjetc.mapper.UserMapper;
import com.tjetc.mapper.UserTribeMapper;
import com.tjetc.service.ITribeMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tjetc.service.IUserTribeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
public class TribeMessageServiceImpl extends ServiceImpl<TribeMessageMapper, TribeMessage> implements ITribeMessageService {

    @Resource
    private TribeMessageMapper tribeMessageMapper;
    @Resource
    private TribeMapper tribeMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserTribeMapper userTribeMapper;

    @Resource
    private IUserTribeService userTribeService;



    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saves(TribeMessage tribeMessage) {
        tribeMessageMapper.insert(tribeMessage);
//        Tribe tribe=tribeMapper.selectById(tribeMessage.getTribeId());
//        tribe.setCount(tribe.getCount()+1);
//        tribeMapper.updateById(tribe);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean exits(TribeMessage tribeMessage1) {
        tribeMessageMapper.deleteById(tribeMessage1.getId());
        QueryWrapper<UserTribe> qw=new QueryWrapper<>();
        qw.eq("user_id",tribeMessage1.getUserId());
        qw.eq("tribe_id",tribeMessage1.getTribeId());
        UserTribe userTribe=userTribeService.getOne(qw);

        userTribeMapper.deletes(userTribe.getTribeId(),userTribe.getUserId());
       Tribe tribe=tribeMapper.selectById(tribeMessage1.getTribeId());
       tribe.setCount(tribe.getCount()-1);
       tribeMapper.updateById(tribe);
        return true;
    }

    @Override
    public PageInfo<TribeMessage> lists(Integer curPage, Integer pageSize, String applyId) {
        PageHelper.startPage(curPage,pageSize);
        List<TribeMessage> list=tribeMessageMapper.list(applyId);
        for (TribeMessage tribeMessage : list) {
            Tribe tribe=tribeMapper.selectById(tribeMessage.getTribeId());
            User user=userMapper.selectById(tribeMessage.getUserId());
            tribeMessage.setUser(user);
            tribeMessage.setTribe(tribe);
        }
        return new PageInfo<>(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean check(Integer id) {


        TribeMessage tribeMessage=tribeMessageMapper.selectById(id);
        tribeMessage.setState(1);
        tribeMessageMapper.updateById(tribeMessage);

        Tribe tribe=tribeMapper.selectById(tribeMessage.getTribeId());
        tribe.setCount(tribe.getCount()+1);
        tribeMapper.updateById(tribe);
        UserTribe userTribe=new UserTribe();
        userTribe.setTribeId(tribeMessage.getTribeId());
        userTribe.setUserId(tribeMessage.getUserId());
        userTribeMapper.insert(userTribe);
        return true;
    }
}
